﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TechnicBlog.Models;

namespace TechnicBlog.Controllers.FireWorkManager
{
    public class FireWorkController : Controller
    {
        //
        // GET: /FireWork/

        public ActionResult Index(int id)
        {
            DBContext db = new DBContext();
            BrandEntity model = db.Brands.Find(id);
            List<CategoryEntity> list = db.Categorys.ToList();
            list.Insert(0,new CategoryEntity(){Id=0,CategoryName="全部类型"});
            SelectList CategoryList = new SelectList(list, "Id", "CategoryName");
            ViewBag.CategoryList = CategoryList;
            return View(model);
        }

        public string List(int id)
        {
            DBContext db = new DBContext();
            int pageindex = Convert.ToInt32(Request.Form["page"]);
            int pagesize = Convert.ToInt32(Request.Form["rows"]);
            string FireWorkName = "";
            int CategoryId = 0;
            if (Request.QueryString["FireWorkName"] != null)
                FireWorkName = Request.QueryString["FireWorkName"].ToString();
            if (Request.QueryString["CategoryId"] != null)
                CategoryId = Convert.ToInt32(Request.QueryString["CategoryId"].ToString());
            IList<FireWorkEntity> list = db.FireWorks.ToList();
            var result = from u in list
                         join c in db.Categorys.ToList() on u.CategoryId equals c.Id into ucs
                         from uc in ucs.DefaultIfEmpty()
                         where u.BrandId.Equals(id)
                         where u.FireWorkName.Contains(FireWorkName)
                         
                         orderby u.FireWorkName ascending
                         select new { 
                            Id = u.Id,
                            BindId = u.BrandId,
                            FireWorkName = u.FireWorkName,
                            Material = u.Material,
                            ProductCode = u.ProductCode,
                            Remark = u.Remark,
                            CategoryName = uc.CategoryName,
                            CategoryId = uc.Id
                         };
            if (CategoryId > 0)
                result = result.Where(t => t.CategoryId.Equals(CategoryId));
            var l = (result).Skip(pagesize * (pageindex - 1)).Take(pagesize).ToList();
            int total = result.Count();
            Newtonsoft.Json.Converters.IsoDateTimeConverter timeConverter = new Newtonsoft.Json.Converters.IsoDateTimeConverter();
            //这里使用自定义日期格式，如果不使用的话，默认是ISO8601格式           
            timeConverter.DateTimeFormat = "yyyy'-'MM'-'dd";
            //jsonObject是准备转换的对象
            string s = "{\"total\":" + total + ",\"rows\":" + Newtonsoft.Json.JsonConvert.SerializeObject(l, Newtonsoft.Json.Formatting.Indented, timeConverter) + "}";
            db.Dispose();
            return s;
        }

        public ActionResult Create(int id,FireWorkEntity model)
        {
            model.BrandId = id;
            DBContext db = new DBContext();
            ViewBag.Brand = db.Brands.Find(id);
            SelectList CategoryList = new SelectList(db.Categorys.ToList(), "Id", "CategoryName");
            ViewBag.CategoryList = CategoryList;
            return View(model);
        }

        public ActionResult Add(FireWorkEntity model)
        {
            try
            {
                DBContext db = new DBContext();
                db.FireWorks.Add(model);
                db.SaveChanges();
                db.Dispose();
                return JavaScript("$('#loading').window('close');$('#divFireWorkEdit').window('close');$.messager.alert('提示','<br>恭喜您！新增成功！','info');$('#rptFireWorkList').datagrid('reload');");
            }
            catch (Exception)
            {
                return JavaScript("$('#loading').window('close');$.messager.alert('错误','<br>新增失败！请检查输入是否有误！','error');");
            }
        }

        public ActionResult Edit(int id)
        {
            DBContext db = new DBContext();
            FireWorkEntity model = db.FireWorks.Find(id);
            ViewBag.Brand = db.Brands.Find(model.BrandId);
            SelectList CategoryList = new SelectList(db.Categorys.ToList(), "Id", "CategoryName",model.CategoryId);
            ViewBag.CategoryList = CategoryList;
            db.Dispose();
            if (model == null)
            {
                return JavaScript("$('#loading').window('close');$('#divFireWorkEdit').window('close');$.messager.alert('提示','<br>该数据不存在或已被删除','info');$('#rptFireWorkList').datagrid('reload');");
            }
            return View(model);
        }

        [HttpPost]
        public ActionResult Edit(FireWorkEntity model)
        {
            try
            {
                DBContext db = new DBContext();
                FireWorkEntity brand = db.FireWorks.Find(model.Id);
                UpdateModel(brand);
                db.SaveChanges();
                db.Dispose();
                return JavaScript("$('#loading').window('close');$('#divFireWorkEdit').window('close');$.messager.alert('提示','<br>恭喜您！编辑成功！','info');$('#rptFireWorkList').datagrid('reload');");
            }
            catch (Exception)
            {
                return JavaScript("$('#loading').window('close');$.messager.alert('错误','<br>编辑失败！请检查输入是否有误！','error');");
            }
        }

        public ActionResult Delete(int id)
        {
            DBContext db = new DBContext();
            int fwId = Convert.ToInt32(Request.QueryString["Id"].ToString());
            FireWorkEntity model = db.FireWorks.Find(fwId);
            if (model == null)
            {
                return JavaScript("$('#loading').window('close');$.messager.alert('提示','<br>要删除的数据不存在，或已被删除！','info'); $('#rptFireWorkList').datagrid('reload');");
            }
            db.FireWorks.Remove(model);
            db.SaveChanges();
            db.Dispose();
            return JavaScript("$('#loading').window('close');$.messager.alert('提示','<br>删除成功！','info');$('#rptFireWorkList').datagrid('reload');");
        }

    }
}
